//
//  CategoryViewController.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/30.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CategoryViewController.h"

#import "CategoryHeaderView.h"

#import "ProductCell.h"

#import "CategorySearchBar.h"

#import "ResultViewController.h"

#import "DetailViewController.h"

@interface CategoryViewController ()<UITableViewDelegate,UITableViewDataSource,CategoryHeaderViewDelegate,CategorySearchBarDelegate>

/**分类id*/
@property(nonatomic,copy)NSString * cat_id;

@property(nonatomic,strong)CategoryModel * category;

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)CategoryHeaderView * headerView;

@property(nonatomic,strong)CategorySearchBar * searchBar;

@property(nonatomic,strong)NSArray * categorys;


@property(nonatomic,strong)NSMutableArray * productArray;


@end

@implementation CategoryViewController
static NSString * productCellID = @"ProductCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(@"#eeeeee");
    self.productArray = [NSMutableArray array];
    self.categorys = [ShareCategory shareCategory].categorys;
    self.headerView.categoryArray = self.categorys;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.headerView];
    [self.tableView reloadData];
    [self getProductWith:self.cat_id];
}

-(void)getProductWith:(NSString*)cat_id{
    [[LXDataManager manager] getProductsWithCatt_id:cat_id complete:^(BOOL success, id res) {
        if(success){
            [self.productArray removeAllObjects];
            [self.productArray addObjectsFromArray:res];
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setUpNavi];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
  
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.headerView hidden];
}

-(void)setUpNavi{
    [super setUpNavi];
    self.navigationItem.titleView = self.searchBar;

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.productArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductCell * cell = [tableView dequeueReusableCellWithIdentifier:productCellID];
    cell.roundStyle =  RoundStyleAll;
    cell.product = self.productArray[indexPath.section];
    [self addCellHeight:cell.cellHeight indexPath:indexPath];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self cellHeightForIndexPath:indexPath];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    CGRect rect = [cell.productImageView convertRect:cell.productImageView.frame toView:self.view.window];
    
    CGRect rect1 = [cell.contentView convertRect:cell.contentView.frame toView:self.view.window];
    CGPoint leftTop = CGPointMake(rect.origin.x>100?rect.origin.x:rect.origin.x, rect1.origin.y);
    DetailViewController * detailVC = [[DetailViewController alloc] init];
    detailVC.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(leftTop.x, leftTop.y, cell.productImageView.width, cell.productImageView.width)];
    detailVC.imageView.image = cell.productImageView.image;
    detailVC.starPoint = leftTop;
    detailVC.product = [self.productArray objectAtIndex:indexPath.section];
    CircleAnimator * animator = [CircleAnimator animatorWithFromImageView:cell.productImageView toImageView:detailVC.imageView];
    detailVC.animator = animator;
    [self.navigationController xw_pushViewController:detailVC withAnimator:animator];

}

-(UITableView*)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:(CGRect){0, self.headerView.height-10,ScreenWidth,ScreenHeight - 64 - self.headerView.height} style:UITableViewStyleGrouped];
        _tableView.sectionFooterHeight = 0;
        _tableView.sectionHeaderHeight = globalMargin;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[ProductCell class] forCellReuseIdentifier:productCellID];
    }
    return _tableView;
}

-(CategoryHeaderView*)headerView{
    if(!_headerView){
        _headerView = [[CategoryHeaderView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, Category_height(51))];
        _headerView.backgroundColor = [UIColor whiteColor];
        _headerView.delegate = self;
        _headerView.cat_id = self.cat_id;
    }
    return _headerView;
}

-(void)setProductArray:(NSMutableArray *)productArray{
    _productArray = productArray;
}


-(void)CategorySearchBar:(CategorySearchBar *)searchBar didClickSerchButton:(UIButton *)searchButton{
    ResultViewController * resVC = [[ResultViewController alloc] initWithKeyword:searchBar.text];
    [self.navigationController pushViewController:resVC animated:YES];
}

-(CategorySearchBar*)searchBar{
    if(_searchBar == nil){
        _searchBar = [[CategorySearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 29)];
        _searchBar.delegate = self;
    }
    return _searchBar;
}



#pragma mark - 弹出菜单回调
-(void)CategoryHeaderView:(CategoryHeaderView *)categoryHeaderView hiddenWithCat_id:(NSString *)cat_id sortType:(NSInteger)sortType priceRange:(NSInteger)priceRane discounts:(NSArray *)discounts{
    
    [[LXDataManager manager] getProductWithPrama:@{@"cat_id":cat_id,@"sort":[NSString stringWithFormat:@"%ld",sortType]} complete:^(BOOL success, id res) {
        if(success){
            self.cat_id = cat_id;
            [self.productArray removeAllObjects];
            [self.productArray addObjectsFromArray:res];
            [self.tableView reloadData];
        }
    }];
    NSLog(@"cat_id--->%@ sortType--->%ld priceRange--->%ld discounts--->%@ ",cat_id,sortType,priceRane,discounts);
}

-(id)initWithCat_id:(NSString *)cat_id{
    if(self = [super init]){
        self.cat_id = cat_id;
    }
    return self;
}

-(id)initWith:(CategoryModel *)category{
    if(self = [super init]){
        self.category = category;
    }
    return self;
}
@end
